#define NUM_LIGHTS 2

varying vec3 normal, eyeVector;
varying vec3 lightDir[NUM_LIGHTS];
uniform sampler2D SurfaceTex;
uniform float texBlend;

vec4 ShadeFragment()
{
	
	vec4 color = gl_FrontLightModelProduct.sceneColor;
	vec4 texel = texture2D(SurfaceTex, gl_TexCoord[0].st);
	vec3 N = normalize(normal);
	
	int i;
	for (i=0; i<NUM_LIGHTS; i++)
	{	
		vec3 L = normalize(lightDir[i]);
		float lambertTerm = dot(N,L);
		color = color + gl_FrontMaterial.ambient * gl_LightSource[i].ambient + gl_LightModel.ambient * gl_FrontMaterial.ambient;	
		if (lambertTerm > 0.0)
		{
		    vec4 diffuseColor = gl_LightSource[i].diffuse * gl_FrontMaterial.diffuse * lambertTerm;
			color.rgb = color.rgb + diffuseColor.rgb;
		}
	}
	
	color = (1.0-texBlend)*color + texBlend*vec4(texel.rgb * color.rgb, texel.a);
	
	for (i=0; i<NUM_LIGHTS; i++)
	{	
		vec3 L = normalize(lightDir[i]);
		float lambertTerm = dot(N,L);
		if (lambertTerm > 0.0)
		{ 
		  vec3 E = normalize(eyeVector);
		  vec3 R = reflect(-L, N);
		  float specular = pow(max(dot(R, E), 0.0), gl_FrontMaterial.shininess);
		  vec4 specColor = gl_LightSource[i].specular * gl_FrontMaterial.specular * specular;
		  color.rgb = color.rgb + specColor.rgb;
		}
	}
	
	return color;
}